package com.ruoyi.project.cms.notice.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.notice.domain.CNotice;
import com.ruoyi.project.cms.notice.domain.Type;
import com.ruoyi.project.cms.notice.service.CNoticeService;
import com.ruoyi.project.cms.notice.service.TypeService;
import com.ruoyi.project.monitor.server.domain.Sys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者:yzc
 * 类名:NoticeController
 * 描述:TODO
 * 时间:2020-05-21
 */
@Controller
@RequestMapping("/notice/cnotice")
public class CNoticeController extends BaseController {

    @Autowired
    private CNoticeService cNoticeService;

    @Autowired
    private TypeService typeService;

    String  prefix="cms/notice";

    @GetMapping("")
    public String type()
    {
        return  prefix+ "/notice";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CNotice cNotice){
        startPage();

        List<CNotice> list = cNoticeService.findAllList(cNotice);

        return getDataTable(list);
    }

    /**
     * 新增公告信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("types", typeService.findMore(new Type()));
        return prefix + "/add";
    }

    /**
     * 保存新增公告信息
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated CNotice cNotice)
    {
        return toAjax(cNoticeService.addNotice(cNotice));
    }

    /**
     * 修改公告信息
     */
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable("noticeId") Integer noticeId, ModelMap mmap)
    {
        CNotice notice = cNoticeService.findById(noticeId);
        mmap.put("notice", cNoticeService.findById(noticeId));
        return prefix + "/edit";
    }


    /**
     * 保存修改后的公告信息
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated CNotice cNotice)
    {
        return toAjax(cNoticeService.updateNotice(cNotice));
    }

    /**
     * 删除公告信息
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removeType(String ids)
    {
        try
        {
            return toAjax(cNoticeService.removeType(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 查看公告信息内容
     */
    @GetMapping("/selNotice/{noticeId}")
    public String selType(@PathVariable("noticeId") Integer noticeId, ModelMap mmap)
    {
        CNotice notice = cNoticeService.findById(noticeId);
        System.out.println(notice);
        List<Type> tps = typeService.typeList();
        mmap.put("tps",tps);
        mmap.put("notice", notice);
        return prefix + "/info";
    }

    @PostMapping("/selDesc")
    @ResponseBody
    public AjaxResult selDesc(CNotice cNotice)
    {
        return toAjax(1);
    }

    @GetMapping("/typeList")
    @ResponseBody
    public AjaxResult typeList(){
        List<Type> list = typeService.typeList();
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",list);
    }
}
